#pragma once

#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include "Log.hpp"
#include "Common.hpp"

using namespace LogModule;

const std::string name = "Dict.txt";
const std::string path = "./";
const std::string symbol = ":";

class Dictionary
{
private:
    bool is_Translate(const std::string &word)
    {
        std::string file = _filepath + _filename;
        std::ifstream in(file.c_str());
        if (!in.is_open())
        {
            LOG(LogLevel::ERROR) << "open file  false";
            exit(1);
        }
        std::string line;
        while (std::getline(in, line))
        {
            std::string key;
            std::string value;
            if(SplitString(line, &key, &value, symbol))
            {
                _dict[key] = value;
            }
        }
        in.close();
        return true;
    }

public:
    Dictionary(std::string filename = name, std::string filepath = path)
        : _filename(filename), _filepath(filepath)
    {
    }
    std::string Translate(const std::string& word)
    {
        if(is_Translate(word))
        {
            return _dict[word];
        }
        return "NULL";

    }
    ~Dictionary()
    {
    }

private:
    std::string _filename;
    std::string _filepath;
    std::unordered_map<std::string, std::string> _dict;
};